//
//  HMMessageController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/9.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMMessageController.h"
#import "MessageCell.h"
#import "MessageModel.h"

@interface HMMessageController () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * messageArray;

@end

@implementation HMMessageController

- (NSMutableArray *)messageArray
{
    if (_messageArray == nil) {
        _messageArray = [NSMutableArray array];
    }
    return _messageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"消息中心";
    
    [self setUpTableView];
    [self setupData];
}

- (void)setUpTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageCell class]) bundle:nil] forCellReuseIdentifier:@"MessageCell"];
}

- (void)setupData
{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObjOrNil:[MGAccountTool getUid] forKey:@"uid"];
    
    [HttpBaseTool post:kMessageUrl params:param success:^(id responseObj) {
        self.messageArray = [MessageModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    cell.message = self.messageArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyLog(@"你选中了%ld",indexPath.row);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
